<?php
namespace app\common\model;

class Gg extends Base
{
    public static function search($id = WEB_ID)
    {
        $page = input('post.page/d');
        $pageSize = input('post.pageSize/d');
        $page = ($page < 1) ? 1 : $page;
        $pageSize = ($pageSize < 1 || $pageSize > 50) ? 10 : $pageSize;
        $self = new static();
        $query = $self->alias('a');
        is_numeric($id) && $query->where('a.web_id','=', $id);
        $query2 = clone $query;
        $list = $query->page($page, $pageSize)->order('a.id desc')->select();
        return [
            'total' => $query2->count('a.id'),
            'list' => $list
        ];
    }
	
    public static function add($data)
    {
        $self = new static();
        $data['web_id'] = WEB_ID;
		
        switch ($data['type']) {
            case 0:
                $validate = validate('Gg');
				$data['url'] = '';
		$data['enddate'] = '';
		$data['color'] = '';
                if (!$validate->scene('add1')->check($data)) {
                    $result = [
                        'message' => $validate->getError(),
                        'code' => 1
                    ];
                } else {
                    $data['date'] = date("Y-m-d");
                    if ($self->field('con,date,type,url,enddate,color,web_id')->insert($data)) {
                        $result = [
                            'message' => '添加公告成功！',
                            'code' => 0
                        ];
                    }
                }
                break;
            case 1:
                $validate = validate('Gg');
                if (!$validate->scene('add2')->check($data)) {
                    $result = [
                        'message' => $validate->getError(),
                        'code' => 1
                    ];
                } else {
                    $data['date'] = date("Y-m-d");
                    if ($self->field('con,date,type,url,enddate,color,web_id')->insert($data)) {
                        $result = [
                            'message' => '添加公告成功！',
                            'code' => 0
                        ];
                    }
                }
                break;
            case 2:
                $validate = validate('Gg');
                if (!$validate->scene('add3')->check($data)) {
                    $result = [
                        'message' => $validate->getError(),
                        'code' => 1
                    ];
                } else {
                    $data['date'] = date("Y-m-d");
                    if ($self->field('con,date,type,url,web_id')->insert($data)) {
                        $result = [
                            'message' => '添加公告成功！',
                            'code' => 0
                        ];
                    }
                }
                break;
        }
        return $result;
    }
	
    public static function delByid($id)
    {
        $self = new static();
        if ($self->where('id','=', $id)->delete()) {
            return true;
        }
        return false;
    }
	
    public static function updateByid($id, $data = [])
    {
        $self = new static();
        return ($self->where('id', '=', $id)
                ->update($data) !== false);
    }
}